﻿// ===============================================================================
// News.cs
// EOS Web Framework - Demo
// ===============================================================================
// Copyright (c) EOS Development Group. All rights reserved.
// ===============================================================================

namespace NewsDemo.Pages
{
    using System;
    using System.Globalization;
    using EOS.Framework.Rules;
    using EOS.Framework.Tools;
    using EOS.Framework.Web.Mvc;
    using EOS.Framework.Web.Mvc.Controls;
    using Microsoft.Practices.Unity;
    using NewsDemo.Data;

    public class News : WebPage
    {
        private const int ItemsPerPage = 3;
        private const string ErrorIcon = "/Templates/Images/Error.png";

        #region Dependencies

        [Dependency]
        public IRecentnessDao RecentnessDao { get; set; }

        [Dependency]
        public IEditorDao EditorDao { get; set; }

        #endregion

        public class NewsForm : Form<News>
        {
            protected override void BeginRender()
            {
                Textbox<string> titleTextbox = new Textbox<string>(this, ErrorIcon, 
                    new NameRule(Properties.Resources.TitleError));
                Attach("titleInput", titleTextbox);

                Textbox<string> sourceTextbox = new Textbox<string>(this, ErrorIcon, 
                    new WebsiteRule(Properties.Resources.SourceError));
                Attach("sourceInput", sourceTextbox);

                Textarea<string> textTextarea = new Textarea<string>(this, ErrorIcon, 
                    new NotNullRule(Properties.Resources.MessageError));
                Attach("textInput", textTextarea);

                if (Submitted && IsValid)               
                {
                    Recentness newRecentness = new Recentness();
                    newRecentness.Title = titleTextbox.Value;
                    newRecentness.Text = textTextarea.Value;
                    newRecentness.Source = sourceTextbox.Value;
                    newRecentness.CreatedBy = Container.EditorDao.Get(1);

                    Container.RecentnessDao.SaveOrUpdate(newRecentness);

                    Reset();
                }
            }
        }

        [Action]
        public void Index()
        {
        }

        protected override void BeginRender()
        {
            int numItems = RecentnessDao.Count(); 

            // Calculates the number of pages for the news.
            int numPages = (int)Math.Ceiling((float)numItems / (float)ItemsPerPage);

            // Get the current page number from the query string
            // The query string has the following format: ?Page=1
            int currentPage = 1;
            int.TryParse(Context.HttpContext.Request.QueryString["Page"], out currentPage);

            currentPage = currentPage.RemainBetween(1, numPages);

            // Set the info text, which show the current page and
            // the number of pages.
            SetValue("pageInfo", Properties.Resources.CurrentPage, currentPage, numPages);

            if (numItems == 0 || currentPage == 1)
            {
                Hide("firstPageLink");
                Hide("prevPageLink");
            }
            else
            {
                Attach("firstPageLink", new Hyperlink("?Page=0"));
                Attach("prevPageLink", new Hyperlink("?Page=" + (currentPage - 1)));
            }

            if (numItems == 0 || currentPage == numPages)
            {
                Hide("nextPageLink");
                Hide("lastPageLink");
            }
            else
            {
                Attach("nextPageLink", new Hyperlink("?Page=" + (currentPage + 1)));
                Attach("lastPageLink", new Hyperlink("?Page=" + (numPages)));
            }

            Markup.Attach("linkItem", new Repeater<News, Recentness>(this, RecentnessDao.Find((currentPage - 1) * ItemsPerPage, ItemsPerPage),
                delegate(IMarkup itemMarkup, Recentness item)
                {
                    itemMarkup.Attach("link", new Hyperlink(item.Source, item.Source));

                    itemMarkup.SetValue("title", item.Title);
                    itemMarkup.SetValue("text", item.Text);
                    itemMarkup.SetValue("created", item.Created.ToString(CultureInfo.CurrentCulture));
                    itemMarkup.SetValue("modified", item.Modified.ToString(CultureInfo.CurrentCulture));
                    itemMarkup.SetValue("editor", item.CreatedBy.Username);
                }));

            if (WebContext.Current.Editor != null)
            {
                Attach("newRecentnessForm", new NewsForm());
            }
            else
            {
                Hide("formBox");
            }
        }
    }
}
